﻿using AntdUI;
using Super.Admin.Global;
using Super.Admin.Utils.ThemeManager;
using Super.Admin.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Super.Admin.Utils.EventAggregator.EventType;
using Super.Admin.Utils.EventAggregator;
using Super.Admin.IServices;
using Super.Admin.Entities;
using Super.Admin.Services;
using Super.Admin.Models.User;
using Super.Admin.Mappers;
using Super.Admin.Models.Role;
using Super.Admin.Models.Department;
using System.Runtime.Intrinsics.Arm;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Super.Admin.Models.Department;
using System.Security.Policy;
using Microsoft.EntityFrameworkCore.Metadata;
using Super.Admin.Models.Permission;

namespace Super.Admin.Views.Page
{
    public partial class DepartmentManagementModify : UserControl
    {
        public List<DepartmentDetailModel> _departmentModels { get; set; } = new();
        public List<DepartmentDetailModel> _allDepartmentModels { get; set; } = new();
        private DepartmentDetailModel _currentDepartmentModel = new();
        private List<UserBaseModel> _userModels = new();
        private string _title;
        private Form _form;
        private bool _isCreateChild;
        public DepartmentManagementModify(Form form, List<DepartmentDetailModel> departmentModels, List<DepartmentDetailModel> allDepartmentModels, List<UserBaseModel> userModels, DepartmentDetailModel currentModel, string title)
        {
            InitializeComponent();
            _form = form;
            _departmentModels = departmentModels;
            _allDepartmentModels = allDepartmentModels;
            _currentDepartmentModel = currentModel;
            _userModels = userModels;
            _title = title;
            this.Load += OnLoad;
        }

        private void OnLoad(object? sender, EventArgs e)
        {
            InitializeControls();
        }

        private void InitializeControls()
        {
            _isCreateChild = false;
            LblTitle.Text = _title;

            // 初始化员工信息列表
            CmbPersonInCharge.Items.Clear();
            foreach (var user in _userModels)
            {
                CmbPersonInCharge.Items.Add(new SelectItem(user.UserName, user.UserId));
            }

            // 初始化父级列表
            CmbParentDep.Enabled = true;
            _departmentModels = new List<DepartmentDetailModel>
            {
                new DepartmentDetailModel{ Id = 0,Name = "Root",DepartmentChild = _departmentModels}
            };
            // 设置父菜单
            CmbParentDep.Items.Clear();
            foreach (var dep in _departmentModels)
            {
                var si = new AntdUI.SelectItem(dep.Name, dep.Id) { };
                // 判断是否包含子元素
                if (dep.DepartmentChild != null && dep.DepartmentChild.Count > 0)
                {
                    SetDepartmentSub(si, dep.DepartmentChild);
                }
                CmbParentDep.Items.Add(si);
            }

            if (_title.Contains("新增子部门"))
            {
                CmbParentDep.Enabled = false;
                CmbParentDep.SelectedValue = _currentDepartmentModel.ParentId;
            }
            else if (_currentDepartmentModel.Id != -1) // 编辑
            {
                TxtName.Text = _currentDepartmentModel.Name;
                TxtRemark.Text = _currentDepartmentModel.Remark;
                BtnSwitchStatus.SelectIndex = BtnSwitchStatus.Items.IndexOf(BtnSwitchStatus.Items.FirstOrDefault(i => int.Parse(i.ID) == _currentDepartmentModel.IsActive));
                CmbPersonInCharge.SelectedValue = _currentDepartmentModel.PersonInChargeId;
                CmbParentDep.SelectedValue = _currentDepartmentModel.ParentId;
            }
        }

        private void SetDepartmentSub(SelectItem si, List<DepartmentDetailModel> permissionChildren)
        {
            var subList = new List<AntdUI.SelectItem>();
            // 找到这个ID的所有子元素
            foreach (var model in permissionChildren)
            {
                var sis = new AntdUI.SelectItem(model.Name, model.Id) { };
                if (model.DepartmentChild != null && model.DepartmentChild.Count > 0)
                {
                    SetDepartmentSub(sis, model.DepartmentChild);
                }
                subList.Add(sis);
            }
            si.Sub = new List<object>();
            foreach (var sub in subList)
            {
                si.Sub.Add(sub);
            }
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void CmbParentDep_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            TxtParentDepPersonInCharge.Text = _allDepartmentModels.FirstOrDefault(a => a.Id == Convert.ToInt32(CmbParentDep.SelectedValue))?.PersonInChargeModel.UserName;
            _currentDepartmentModel.ParentId = Convert.ToInt32(CmbParentDep.SelectedValue);
        }

        private void TxtName_TextChanged(object sender, EventArgs e)
        {
            _currentDepartmentModel.Name = TxtName.Text;
        }

        private void CmbPersonInCharge_SelectedValueChanged(object sender, ObjectNEventArgs e)
        {
            _currentDepartmentModel.PersonInChargeId = (int?)CmbPersonInCharge.SelectedValue;
        }

        private void BtnSwitchStatus_SelectIndexChanged(object sender, IntEventArgs e)
        {
            _currentDepartmentModel.IsActive = int.Parse(BtnSwitchStatus.Items[e.Value].ID);
        }

        private void TxtRemark_TextChanged(object sender, EventArgs e)
        {
            _currentDepartmentModel.Remark = TxtRemark.Text;
        }
    }
}